#include "ConvertOp.h"


ConvertOp::ConvertOp() : GenericOperation(qApp->translate("Operations", "Convert Image").toStdString())
{
}

void ConvertOp::operator()(const genericinterface::ImageWindow* currentWnd, const std::vector<const genericinterface::ImageWindow*>&){
    genericinterface::ImageWindow::Mode mode= genericinterface::ImageWindow::USER;


    ConvertOpDialog* dialog = new ConvertOpDialog(NULL,currentWnd);

    QDialog::DialogCode code = static_cast<QDialog::DialogCode>(dialog->exec());

    if(code!=QDialog::Accepted) {
        return;
    }

    ParameterRep newrep;

    int choice = dialog->getRound();
    bool makeop = dialog->getOp();

    const StandardImageWindow * siw= dynamic_cast<const StandardImageWindow *>(currentWnd);
    const DoubleImageWindow * diw= dynamic_cast<const DoubleImageWindow *>(currentWnd);

    if(makeop){
        if(currentWnd->isStandard()){
            StandardImageWindow * siw2 = const_cast<StandardImageWindow *>(siw);
            outDoubleImage(Converter<Image_t<double> >::convert(*(siw2->getImage())),"");
        }else if(currentWnd->isDouble()){
            switch(choice){
                case -1: //error
                    ;break;
                case ParamRep::CEIL  : newrep = {0,ParamRep::STANDARDROUND,ParamRep::CEIL,0,0,0,0,0,0,0};break;
                case ParamRep::ROUND : newrep = {0,ParamRep::STANDARDROUND,ParamRep::ROUND,0,0,0,0,0,0,0};break;
                case ParamRep::TRUNC : newrep = {0,ParamRep::STANDARDROUND,ParamRep::TRUNC,0,0,0,0,0,0,0};break;
                case ParamRep::FLOOR : newrep = {0,ParamRep::STANDARDROUND,ParamRep::FLOOR,0,0,0,0,0,0,0};break;
            default : break;
            }
            ParamRep * param = new ParamRep(newrep);
            DoubleImageWindow * diw2 = const_cast<DoubleImageWindow *>(diw);
            const Image_t<double> * temp2 = diw2->getImage();
            Image_t<double> * temp =  temp2->applyProcessing(param);
            outImage(Converter<Image_t<int> >::convert(*temp),"");
        }
    }

}
